<?php
use com\masfletes\db\DBUtil;

class OperationController_PointsController extends JController {
    
    private $userSessionId;

    public function init() {
        parent::init();
        if (!Model3_Auth::isAuth()) {
            $this->redirect('Index/index');
        }
        $this->userSessionId = $_SESSION['USERSESSIONID'];
    }

    public function getPointByNameAction() {
        $params = $this->getRequest()->getPostJson();
        $pointName = $this->getArrayValue('param1', $params);

        try {
            $this->permitirAcceso($this->userSessionId, 'rutas', 'listar');
            $conn = $this->getConnection();
            $parameters = array($pointName);
            $resultsets = DBUtil::executeStoredProc($conn, 'sp_getPointByName', $parameters, PDO::FETCH_NAMED);
            unset($conn);
        } catch (PDOException $ex) {
            $errMsg = $ex->getMessage(); //utf8_encode($ex->getMessage());
            if (strpos($errMsg, '50000') > -1) {
                echo json_encode(array("error" => trim(substr($errMsg, strpos($errMsg, '50000') + 5)), "code" => $ex->getCode(), "trace" => $ex->getTraceAsString()));
            } else {
                echo json_encode(array("error" => $errMsg, "code" => $ex->getCode(), "trace" => $ex->getTraceAsString()));
            }
            return;
        } catch (Exception $generalEx) {
            echo json_encode(array("error" => $generalEx->getMessage(), "code" => $generalEx->getCode(), "trace" => $generalEx->getTraceAsString()));
            return;
        }
        echo json_encode($resultsets);
    }
}
